package com.plian.system.controller.bpa;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.plian.Tools.Func;
import com.plian.Tools.StringPool;
import com.plian.Tools.base.BaseController;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.config.Resubmit;
import com.plian.system.entity.bpa.BusinessAssessment;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.search.SearchEntity;
import com.plian.system.mybatis.search.SearchParamEntiy;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.bpa.IBusinessAssessmentService;
import com.plian.system.vo.bpa.BusinessAssessmentVO;
import com.plian.system.wrapper.bpa.BusinessAssessmentWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 经营考核配置-新
 *
 * @Description:
 * @Author: jatpen
 * @Date: 2023/7/27 9:42 AM
 */
@RestController
@RequestMapping("api/bpabusinessassessment")
@AllArgsConstructor
@Api(value = "经营考核配置-新", tags = "经营考核配置-新")
public class BusinessAssessmentController extends BaseController {

    private IBusinessAssessmentService businessAssessmentService;

    private BusinessAssessmentWrapper businessAssessmentWrapper;


    /**
     * 详情
     */
    @PostMapping("/detail")
    @ApiOperation(value = "详情", notes = "传入id")
    public R<BusinessAssessmentVO> detail(@Valid @RequestBody BusinessAssessment businessAssessment) {
        BusinessAssessment assessment = businessAssessmentService.getOne(Condition.getQueryWrapper(businessAssessment));
        return R.data(businessAssessmentService.detail(assessment));
    }

    /**
     * 分页
     */
    @PostMapping("/list")
    @ApiOperation(value = "详情", notes = "传入query")
    public ListResult<BusinessAssessmentVO> list(@RequestBody Query query) {
        List<SearchEntity> entityList = query.getSearch();
        if (query.getAscs()==null&&query.getDescs()==null){
            query.setDescs("create_time");
        }
        QueryWrapper<BusinessAssessment> queryWrapper = SearchParamEntiy.getQueryWrapper(Condition.getQueryWrapper(new BusinessAssessment()), entityList);
//        TokenSearch<BusinessAssessment> tokenSearch = new TokenSearch<>();
//        tokenSearch.addTokenQuery(queryWrapper, query);
        IPage<BusinessAssessment> pages = businessAssessmentService.page(Condition.getPage(query), queryWrapper);
        List<BusinessAssessmentVO> vehicleLeaseVOS  = businessAssessmentWrapper.entityToVO(pages.getRecords());
        ListResult<BusinessAssessmentVO> listResult = new ListResult<>(CommonCode.SUCCESS);
        listResult.setList(vehicleLeaseVOS);
        listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
        return listResult;
    }

    /**
     * 新增或修改
     */
    @PostMapping("/submit")
    @Resubmit(delaySeconds = 0)
    @LogOperation("经营考核配置-提交")
    @ApiOperation(value = "新增或修改", notes = "新增或修改")
    public R submit(@Valid @RequestBody BusinessAssessmentVO businessAssessmentVO) {
        return businessAssessmentService.submit(businessAssessmentVO);
    }


    /**
     * 删除
     */
    @PostMapping("/remove")
    @LogOperation("经营考核配置-删除")
    @ApiOperation(value = "删除", notes = "传入ids")
    public R remove(@RequestBody PageData pd) {
        List<String> ids = (List<String>)pd.get("ids");
        return businessAssessmentService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA)))
                ? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
    }

    /**
     * 应用/失效
     */
    @PostMapping("/enabled")
    @LogOperation("经营考核配置-应用/失效")
    @ApiOperation(value = "应用/失效 ", notes = "传入BusinessAssessment-enabled(1/2)")
    public R enable(@RequestBody BusinessAssessment businessAssessment) {
        return businessAssessmentService.updateById(businessAssessment)
                ? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
    }
}
